package main

import (
	"time"

	"math/rand"

	"gitee.com/wu-jin-feng/fgin"
	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
	// 计数器
	httpRequestsTotal = prometheus.NewCounter(prometheus.CounterOpts{
		Name: "prometheus_test_web",
		Help: "Total number of HTTP requests.",
	})
	// 直方图
	httpHistogram = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "prometheus_test_web_Histogram",
			Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
		},
		[]string{"method", "path"})
	// 仪表盘
	httpGauge = prometheus.NewGauge(prometheus.GaugeOpts{
		Name: "prometheus_test_web_Gauge",
	})
)

func init() {
	prometheus.MustRegister(httpRequestsTotal)
	prometheus.MustRegister(httpHistogram)
	prometheus.MustRegister(httpGauge)
}

func main() {
	httpRequestsTotal.Inc()
	f := fgin.Fgin{}
	r := f.GetHttpServer("./test.yaml")
	r.GET("/ping1", func(ctx *gin.Context) {
		// 计数器+1
		httpRequestsTotal.Inc()
		// 仪表设置为10
		httpGauge.Set(10)
		// 请求时间分布直方图
		defer prometheus.NewTimer(httpHistogram.WithLabelValues("post", "/ping")).ObserveDuration()
		flag := rand.Intn(2)
		time.Sleep(time.Duration(flag) * time.Second)
		ctx.JSONP(200, "success")
	})
	// 暴露给prometheus
	r.GET("/metrics", gin.WrapH(promhttp.Handler()))
	f.Run(r)
}
